package com.gp.testonline.controller;

import com.gp.testonline.eo.PersonEOPage;
import com.gp.testonline.model.Person;
import com.gp.testonline.service.PersonService;
import com.gp.testonline.tool.ResponseMessage;
import com.gp.testonline.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "person")
@RestController
@CrossOrigin
public class PersonController {
    @Autowired
    private PersonService personService;

    @ApiOperation(value = "人员信息添加方法")
    @RequestMapping(value = "/insertPerson",method = RequestMethod.GET)
    public ResponseMessage insert(Person person){
        int IsInsert=0;
        //传入数据的判断
        //(1) teacher不能为空
        if (person!=null){
            //(2) 身份证号不能为空
            if (person.getPidcard()!=null){
                //(3) 添加班级数据
                IsInsert=personService.InsertPerson(person);
                if (IsInsert==1){
                    return Result.success("添加成功！");
                }else if (IsInsert==-1){
                    return Result.error("身份证号重复不允许添加！");
                }else{
                    return Result.error("保存失败！");
                }
            }
        }
        return Result.error("保存失败！");
    }

    @ApiOperation(value = "人员信息修改方法")
    @RequestMapping(value ="/updatePerson",method = RequestMethod.GET)
    public ResponseMessage updatePerson(Person person){
        int isUpdate=0;
        //传入的参数不能为空
        if (person!=null){
            //参数当中主键不能为0
            if (person.getPid()!=0){
                isUpdate=personService.UpdatePerson(person);
                if (isUpdate==1){
                    return Result.success("更新成功！");
                }else {
                    return Result.error("更新失败！身份证号重复！");
                }
            }else {
                return Result.error("传入的参数不正确！");
            }
        }
        return Result.error("传入的参数不正确！");
    }

    @ApiOperation(value = "人员信息删除方法")
    @RequestMapping(value = "/deletePerson",method = RequestMethod.GET)
    public ResponseMessage deletePerson(int pid){
        int isDelete=0;
        if (pid!=0){
            isDelete=personService.DeletePerson(pid);
            if (isDelete>0){
                return Result.success("删除成功！");
            }else{
                return Result.error("删除失败！");
            }
        }else {
            return Result.error("删除的主键不能为0，请重新输入！");
        }
    }

    @ApiOperation(value = "根据条件获取人员详细信息")
    @RequestMapping(value = "/getPerson",produces = "application/json;charest=utf-8",method = RequestMethod.POST)
    public ResponseMessage getPerson(PersonEOPage personEOPage) {
        Map<String, List> map = new HashMap<>();
        List<PersonEOPage> listPerson = personService.getPersonAll(personEOPage);
        map.put("list",listPerson);
        return Result.success("200",map);
    }

    @ApiOperation(value = "登陆查询")
    @RequestMapping(value = "/getLogin",produces = "application/json;charest=utf-8",method = RequestMethod.POST)
    public ResponseMessage getLogin(String paccount,String ppassword) {
        Map<String, List> map = new HashMap<>();
        List<PersonEOPage> listLogin = personService.getLogin(paccount, ppassword);
        if (!listLogin.isEmpty()) {
            map.put("list", listLogin);
            return Result.success("200", map);
        }else {
            return Result.error("用户名或密码错误");
        }
    }

    @ApiOperation(value = "注册添加账号密码")
    @RequestMapping(value = "/insertUsers",method = RequestMethod.GET)
    public ResponseMessage insertUsers(Person person){
        int IsInsert=0;
        //传入数据的判断
                IsInsert=personService.InsertUsers(person);
                if (IsInsert==1){
                    return Result.success("注册成功！请完善信息！");
                }else if (IsInsert==-1){
                    return Result.error("账号号重复不允许添加！");
                }else{
                    return Result.error("注册失败！");
                }
    }

    @ApiOperation(value = "人员id查询")
    @RequestMapping(value = "/getPid",produces = "application/json;charest=utf-8",method = RequestMethod.GET)
    public ResponseMessage getPid(String paccount,String ppassword) {
        String listPid = personService.GetPid(paccount, ppassword);
        if (listPid!=null) {
            return Result.success("200", listPid);
        }else {
            return Result.error("未查到");
        }
    }
}
